<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="开始日期" prop="startDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.startDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开始日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束日期" prop="endDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.endDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="项目负责人" prop="leader">
        <el-input
          v-model="queryParams.leader"
          placeholder="请输入项目负责人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合作单位id" prop="cooperationId">
        <el-input
          v-model="queryParams.cooperationId"
          placeholder="请输入合作单位id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择您的项目状态" clearable :style="{width: '100%'}">
          <el-option
            v-for="dict in dict.type.project_state"
            :key="dict.value"
            :label="dict.label"
            :value="Number(dict.value)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="项目阶段" prop="stage">
        <el-select v-model="queryParams.stage" placeholder="请选择您的项目阶段" clearable :style="{width: '100%'}">
          <el-option
            v-for="dict in dict.type.project_stage"
            :key="dict.value"
            :label="dict.label"
            :value="Number(dict.value)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核专家名" prop="reviewer">
        <el-input
          v-model="queryParams.reviewer"
          placeholder="请输入审核专家名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:projectInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:projectInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:projectInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:projectInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="projectInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="projectId" />
      <el-table-column label="项目类别" align="center" prop="projectType" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.project_type" :value="scope.row.projectType"/>
        </template>
      </el-table-column>
      <el-table-column label="项目方向" align="center" prop="direction" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.exp_direction" :value="scope.row.direction"/>
        </template>
      </el-table-column>
      <el-table-column label="项目名称" align="center" prop="name" />
      <el-table-column label="提交时间" align="center" prop="submitTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.submitTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="项目状态" align="center" prop="state" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.project_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="项目阶段" align="center" prop="stage">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.project_stage" :value="scope.row.stage" v-if="scope.row.state !== 0"/>
          <div v-else>未预评估</div>
        </template>
      </el-table-column>
      <el-table-column label="审核专家名" align="center" prop="reviewer" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:projectInfo:edit']"
          >查看或修改项目</el-button>
          <el-button
            v-if="scope.row.state === 0"
            size="mini"
            type="text"
            icon="el-icon-add"
            @click="handleApplicate(scope.row)"
            v-hasPermi="['system:projectInfo:edit']"
          >申报项目</el-button>
          <el-button
            v-if="scope.row.stage === 3"
            size="mini"
            type="text"
            icon="el-icon-add"
            @click="handleAccept(scope.row)"
            v-hasPermi="['system:projectInfo:edit']"
          >验收项目</el-button>
          <el-button
            v-if="scope.row.stage === 2"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleTrack(scope.row)"
            v-hasPermi="['system:projectInfo:remove']"
          >实施跟踪</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目申报对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="项目类别" prop="projectType">
          <el-select v-model="form.projectType" placeholder="请选择您的项目类别" clearable :style="{width: '100%'}">
            <el-option
              v-for="dict in dict.type.project_type"
              :key="dict.value"
              :label="dict.label"
              :value="Number(dict.value)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="项目方向" prop="direction">
          <el-select v-model="form.direction" placeholder="请选择您的方向" clearable :style="{width: '100%'}">
            <el-option
              v-for="dict in dict.type.exp_direction"
              :key="dict.value"
              :label="dict.label"
              :value="Number(dict.value)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="项目名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="项目实施地点" prop="location">
          <el-input v-model="form.location" placeholder="请输入项目实施地点" />
        </el-form-item>
        <el-form-item label="开始日期" prop="startDate">
          <el-date-picker clearable size="small"
            v-model="form.startDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束日期" prop="endDate">
          <el-date-picker clearable size="small"
            v-model="form.endDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="经济行业领域" prop="economicField">
          <el-input v-model="form.economicField" placeholder="请输入经济行业领域" />
        </el-form-item>
        <el-form-item label="细分领域1">
          <el-input v-model="form.fieldOne" placeholder="请输入第一个细分领域" />
        </el-form-item>
        <el-form-item label="细分领域2">
          <el-input v-model="form.fieldTwo" placeholder="请输入第二个细分领域" />
        </el-form-item>
        <el-form-item label="项目总投资" prop="investment">
          <el-input v-model="form.investment" placeholder="请输入项目总投资" />
        </el-form-item>
        <el-form-item label="申请专项经费" prop="specialMoney">
          <el-input v-model="form.specialMoney" placeholder="请输入申请专项经费" />
        </el-form-item>
        <el-form-item label="项目负责人" prop="leader">
          <el-input v-model="form.leader" placeholder="请输入项目负责人" />
        </el-form-item>
        <el-form-item label="合作单位id" prop="cooperationId">
          <el-input v-model="form.cooperationId" :disabled="true" placeholder="未有人合作" />
        </el-form-item>
        <el-form-item label="主要研究内容与目标">
          <editor v-model="form.researchContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="创新点与关键技术" prop="innovationPoint">
          <el-input v-model="form.innovationPoint" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="现有基础和实施条件" prop="basics">
          <el-input v-model="form.basics" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="预期结果与经济、社会效益" prop="expectation">
          <el-input v-model="form.expectation" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="考核指标" prop="assessIndicators">
          <el-input v-model="form.assessIndicators" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="主要研究人员id" prop="researchers">
          <el-input v-model="form.researchers" placeholder="请输入主要研究人员id" />
        </el-form-item>
        <el-form-item label="项目可行性报告">
          <file-upload v-model="form.feasibilityReport"/>
        </el-form-item>
        <el-form-item label="附件清单">
          <file-upload v-model="form.attachment"/>
        </el-form-item>
        <el-form-item label="当前项目状态" prop="state">
          <el-select v-model="form.state" :disabled="true" clearable :style="{width: '100%'}">
            <el-option
              v-for="dict in dict.type.project_state"
              :key="dict.value"
              :label="dict.label"
              :value="Number(dict.value)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="项目阶段" prop="stage">
          <el-select v-model="form.stage" :disabled="true" clearable :style="{width: '100%'}">
            <el-option
              v-for="dict in dict.type.project_stage"
              :key="dict.value"
              :label="dict.label"
              :value="Number(dict.value)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="审核专家名" prop="reviewer">
          <el-input v-model="form.reviewer" readonly placeholder="系统将分配专家" />
        </el-form-item>
        <el-form-item label="审核意见" prop="reviewResult">
          <el-input v-model="form.reviewResult" readonly type="textarea" placeholder="待审核" />
        </el-form-item>
        <el-form-item label="项目进度计划" prop="projectPlan">
          <el-input v-model="form.projectPlan" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改实施跟踪对话框 -->
    <el-dialog :title="trackTitle" :visible.sync="trackOpen" width="500px" append-to-body>
      <el-form ref="trackForm" :model="trackForm" :rules="rules" label-width="80px">
        <el-form-item label="项目id" prop="projectId">
          <el-input v-model="trackForm.projectId" readonly placeholder="请输入项目id" />
        </el-form-item>
        <el-form-item label="实时跟踪记录" prop="comment">
          <el-input v-model="trackForm.comment" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="附件">
          <file-upload v-model="trackForm.attachment"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitTrackForm">提交记录</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProjectInfo, getProjectInfo, delProjectInfo, addProjectInfo, updateProjectInfo } from "@/api/system/projectInfo";
import {addTracking, updateTracking} from "@/api/system/tracking";

export default {
  name: "ProjectInfo",
  dicts: ['project_state', 'exp_direction', 'project_type', 'project_stage'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目申报表格数据
      projectInfoList: [],
      // 弹出层标题
      title: "",
      // track title
      trackTitle: "",
      // 是否显示弹出层
      open: false,
      //track是否弹出
      trackOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectType: null,
        direction: null,
        name: null,
        location: null,
        startDate: null,
        endDate: null,
        economicField: null,
        fieldOne: null,
        fieldTwo: null,
        investment: null,
        specialMoney: null,
        leader: null,
        cooperationId: null,
        researchContent: null,
        innovationPoint: null,
        basics: null,
        expectation: null,
        assessIndicators: null,
        researchers: null,
        feasibilityReport: null,
        attachment: null,
        submitTime: null,
        state: null,
        stage: null,
        reviewer: null,
        reviewResult: null,
        projectPlan: null,
      },
      // 表单参数
      form: {},
      //实时跟踪表格
      trackForm: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    typeFormatter (row, column) {
      return this.selectDictLabel(this.dict.type.project_type, row.projectType);
    },
    dirFormatter (row, column) {
      return this.selectDictLabel(this.dict.type.exp_direction, row.direction);
    },
    stateFormatter (row, column) {
      return this.selectDictLabel(this.dict.type.project_state, row.state);
    },
    stageFormatter (row, column) {
      return this.selectDictLabel(this.dict.type.project_stage, row.stage);
    },
    /** 查询项目申报列表 */
    getList() {
      this.loading = true;
      listProjectInfo(this.queryParams).then(response => {
        this.projectInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.trackOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        projectId: null,
        projectType: null,
        direction: null,
        name: null,
        location: null,
        startDate: null,
        endDate: null,
        economicField: null,
        fieldOne: null,
        fieldTwo: null,
        investment: null,
        specialMoney: null,
        leader: null,
        cooperationId: null,
        researchContent: null,
        innovationPoint: null,
        basics: null,
        expectation: null,
        assessIndicators: null,
        researchers: null,
        feasibilityReport: null,
        attachment: null,
        submitTime: null,
        state: null,
        stage: null,
        reviewer: null,
        reviewResult: null,
        projectPlan: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    resetTrack() {
      this.trackForm = {
        trackId: null,
        projectId: null,
        comment: null,
        attachment: null,
        createBy: null,
        createTime: null
      };
      this.resetForm("trackForm");
    },
    handleApplicate(row) {
      row.state = 1;
      updateProjectInfo(row).then(response => {
        this.$modal.msgSuccess("申报成功");
        this.getList();
      });
    },
    handleAccept(row) {
      updateProjectInfo(row).then(response => {
        this.$modal.msgSuccess("申请成功");
        this.getList();
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.projectId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const projectId = row.projectId || this.ids
      getProjectInfo(projectId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改项目申报";
      });
    },
    handleDelete(row) {

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.projectId != null) {
            updateProjectInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProjectInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮 */
    submitTrackForm() {
      this.$refs["trackForm"].validate(valid => {
        if (valid) {
          if (this.trackForm.trackId != null) {
            updateTracking(this.trackForm).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.trackOpen = false;
              this.getList();
            });
          } else {
            addTracking(this.trackForm).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.trackOpen = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 实时跟踪 */
    handleTrack(row) {
      this.trackOpen = true;
      this.trackForm.projectId = row.projectId;
      this.trackTitle = '实施跟踪记录'
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/projectInfo/export', {
        ...this.queryParams
      }, `projectInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
